import React from 'react';

import Header from '../common/Header';
import CKModal from '../components/CKModal';
import Validate from '../common/Validate';

import {
    Button,
    Input,
    Form,
} from 'amazeui-react';

import Fetch from '../common/Fetch';

import $ from 'jquery';

class LinkCategoryEdit extends React.Component {
    constructor(props) {
        super(props);
        this.state = {
            data: {
                ctg_id: '',
                ctg_name: '',
            },
            is_save: false,
            type: this.props.params.id ? 'edit' : 'add'
        }
    }

    componentDidMount() {
        if (this.state.type == 'edit') {
            this.loadCategory()
        }
    }

    loadCategory() {
        Fetch('/cms/link_manage/category_info', {cipher_id: this.props.params.id}, (res)=> {
            if (res.status) {
                let data = this.state.data;
                $.map(res.data, (item, index)=> {
                    data[index] = item
                })
                this.setState({
                    data: data
                });
            } else {
                this.modal.alert('错误', '获取分类信息失败');
            }
        });
    }

    changeHandler(field){
        return (e)=>{
            let info = this.state.data;
            info[field] = e.target.value;
            this.setState({
                data:info
            });
        }
    }

    checkData() {
        let valid = {
            ctg_name: {role: '*', errmsg: '分类名称不能为空'},
        }
        let data = this.state.data;
        try {
            Validate.inst().checkData(data, valid);
            return data;
        } catch (e) {
            this.modal.alert('错误', e);
        }
    }

    save = ()=>{
        let data = this.checkData();
        if(data === false || this.state.is_save) return;
        this.setState({
            is_save:true
        });
        Fetch('/cms/link_manage/save_category', {data: data}, (res)=> {
            if (res.status) {
                this.modal.alert('成功', '保存分类成功!', ()=> {
                    this.context.router.goBack();
                });
            } else {
                this.modal.alert('失败', '保存分类失败!!');
                this.setState({
                    is_save: false
                });
            }
        }, ()=> {
            this.modal.alert('失败', '远程调用失败!!');
            this.setState({
                is_save: false
            });
        });
    }


    render() {
        let data = this.state.data;
        return (
            <div className="content">
                <Header title={this.state.type === 'edit' ? '修改链接分类' : '添加链接分类'} back={true}/>
                <div className="content_main">
                    <div style={{marginBottom: '20px'}}>
                        <h4 className="title">分类信息</h4>
                    </div>
                    <Form ref="from" horizontal style={{marginTop: '20px'}}>
                        <Input ref="ctg_name" label="分类名称"
                               labelClassName="am-u-sm-2"
                               wrapperClassName="am-u-sm-4 am-u-end"
                               value={data.ctg_name}
                               placeholder="请输入分类"
                               onChange={this.changeHandler('ctg_name')}
                        />
                    </Form>
                    <div className="am-form-group">
                        <div className="am-u-sm-2">
                            &nbsp;
                        </div>
                        <div className="am-u-sm-4 am-u-end">
                            <Button amStyle="primary"
                                    onClick={this.save}
                                    disabled={this.state.is_save}
                                    radius
                            >
                                {this.state.is_save ? '保存中...' : '保存'}
                            </Button>
                        </div>
                    </div>
                </div>
                <CKModal ref={(c)=> {
                    this.modal = c
                }}/>
            </div>
        );
    }
}
LinkCategoryEdit.contextTypes = {
    router:React.PropTypes.object
}
export default LinkCategoryEdit;